/***
 *
 * 一个用于实现拖拽标记的标签类 singer.js
 *
 * Daniel
 * 2018.03.13
 *
 * **/
define(["objmodel"],function(objmodel){
    
    var Singer = function(Driver,Handle){
        this.pick = Handle || function(){};
        this.parse = objmodel.parse;
        
        this.tagLable = {};
        this.tagLine = {};
        this.tagPoint = {};
        this.endPoint = {};
        this.world = Driver;
        this.scene = Driver.scene;
        this.coord = {};
        this.tagArray = {};
        this.temporary = {};
        
        this.tagLineAr = [];
        this.tagLableAr = [];
        this.tagLableGroup = [];
        this.tagLineArGroup = [];
        
        this.count = 0;
        this.name = '';
        this.point = '';
        
        this.flag = true;
        
        scope = this;
    };
    
    Object.assign(Singer.prototype,{

//绘制标记
   graphic : function (){
        var scope = this;
        return function (event) {
            var project = scope.pick.pick()(event);
            var flags = false;
            if(project == undefined)return flags;
            var points = project[0].point;
            var name = "PO"+Math.floor(Math.random()*10000);
            var id =  scope.count;
            scope.tagLable[""+name+""] = points;
            scope.tagLableAr[id] = points;
            scope.count++;
            scope.tagArray.name = name;
            scope.endPoint = "";
            flags = true;
            return  flags;
        };
        
    },

//标记预览
    grapgraphicMove : function (){
        var scope = this;
        var scene = this.scene;
        return function (event) {
            var startN = scope.tagArray.name;
            var startPoint = scope.tagLable[startN];
            var endPoint = scope.graphicLinePoint(event,startPoint).clone();
            var tagLines = scope.lineDraw(startPoint,endPoint);
            var id = tagLines.id;
            var name = "tagLine"+id;
            tagLines.name = name;
            scope.tagLine[""+name+""] = tagLines;
            if(scope.tagLineAr.length>0){
                var ddLine = scope.tagLineAr[0];
                scene.remove(ddLine);
                scope.tagLineAr = [];
            }
            scene.add(tagLines);
            scope.tagLineAr.push(tagLines);
            scope.tagPoint[""+startN+""] = endPoint;
            scope.endPoint = endPoint;
        };
    },


//绘制二维框
    drawDiv : function (value,className){
        this.className = className;
        this.value = value;
    },


//三维坐标转换
   graphicTag : function (pointV){
        var backData = {
            x: '',
            y: ''
        };
        var world = new THREE.Vector3(pointV.x,pointV.y,pointV.z);
        var vector = world.clone().project(this.world.camera.clone());
        
        var halfWidth = window.innerWidth / 2;
        var halfHeight = window.innerHeight / 2;
        //转换后的二维坐标
        var wx = Math.round((vector.x+1) * halfWidth);
        var wy = Math.round(-(vector.y-1) * halfHeight);
        
        backData.x = wx;
        backData.y = wy;
        
        
        return backData
    },
    
    graphicShow : function (){
        var scope = this;
        return function () {
            var destination = scope.endPoint;
            if(!destination){
                scope.flag = false;
                return;
            }
            scope.flag = true;
            var coordPoint = scope.graphicTag(destination);
            scope.coord = coordPoint;
            scope.tagLineArGroup.push(scope.tagLineAr[0]);
            scope.temporary = {
                startPoint: scope.tagLable[scope.tagArray.name],
                endPoint:  scope.endPoint,
                line: scope.tagLineAr[0].toJSON(),
                domId: scope.tagArray.name,
                className: scope.className,
            };
            scope.tagLineAr = [];
            scope.graphicLabelCoord(scope);
        };
    },
    
    graphicLabelCoord : function (tagArray,values){
        var value = values || this.value;
        if(value!=='') {
            var object = tagArray;
            var lable = document.createElement("div");
            lable.id = object.tagArray.name;
            lable.className = object.className;
            lable.innerHTML += value;
            
            lable.style.left = object.coord.x + "px";
            lable.style.top = object.coord.y + "px";
            
            document.body.appendChild(lable);
            this.tagLableGroup.push(lable);
        }
    },


//移除标记
    graphicLabeCancel : function (){
        var scope = this;
        var scene = this.scene;
        return function () {
            if(scope.tagLineAr.length>0){
                var tag = scope.tagLineAr[0];
                scene.remove(tag);
            }
            
        }
    },


//获取line起始点
    graphicLinePoint : function (event,point){
        var target;
        var rect = this.world.renderer.domElement.getBoundingClientRect();
        var x = ( ( event.clientX - rect.left ) / rect.width  ) * 2 - 1;
        var y = -( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
        var dice = new THREE.Vector3(x, y, 0);
        target = dice.unproject(this.world.camera.clone()).clone();
        ///var pp = point.clone().addScaledVector(target,1).divideScalar(1);
        return target;
    },


//几何数据画线
   lineDraw : function (start,end){
        var mat = new THREE.MeshBasicMaterial({color: 0xff0000, transparent: true, opacity: 0.6});
        var geom = new THREE.Geometry();
        var startV = start.clone();
        var endV = end.clone();
        geom.vertices.push(startV,endV);
        var tube = new THREE.Line(geom,mat);
        
        return tube;
    },

//标记解析
   parseSinger : function () {
        var scope = this;
        return function (JSONtexts) {
            var data = JSONtexts.line;
            var singerData = scope.parse(data);
            var value = JSONtexts.name;
            var point = JSONtexts.endPoint;
            var object = {
                tagArray:{
                    name:JSONtexts.domId
                },
                coord:scope.graphicTag(point),
                className: JSONtexts.className
                
            };
            scope.tagPoint[""+JSONtexts.domId+""] = point;
            scope.graphicLabelCoord(object,value);
            scope.scene.add(singerData);
        };
        
        
    },
    

//标记跟随
   tagFollow : function (){
        var scope = this;
        return function(){
            if(scope.tagLableGroup.length > 0 ){
                var length = scope.tagLableGroup.length;
                for(var i = 0; i < length; i++){
                    var domss = scope.tagLableGroup[i];
                    var name = domss.id;
                    var points = scope.tagPoint[name];
                    var cord = scope.graphicTag(points);
                    domss.style.left = cord.x + "px";
                    domss.style.top = cord.y+ "px";
                }
            }
        };
        
    },
    
    
});

    var scope;
    
    
    return Singer;
    
});


